package com.rgs.se.concurrent.blockingQueue;

import java.util.concurrent.*;

/**
 * @author Ren Gaoshuai
 * @ClassName TestMain
 * @Description TODO
 * @date 2021-09-01 22:44
 **/
public class TestMain {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10);

        Producer producer1 = new Producer(queue);
        Producer producer2 = new Producer(queue);
        Producer producer3 = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        //Executors 创建线程 不推荐
        ExecutorService service = Executors.newCachedThreadPool();
        //手动创建线程池 ThreadPoolExecutor的父类Executors
        /**
         *  核心线程数
         *  最大线程数
         *  最大等待时间
         *  时间单位
         *  等待队列BlockingQueue
         *  工厂
         *  饱和策略
         */
        //ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor();

        service.execute(producer1);
        service.execute(producer2);
        service.execute(producer3);
        service.execute(consumer);

        // 执行10s
        Thread.sleep(10 * 1000);
        producer1.stop();
        producer2.stop();
        producer3.stop();
        Thread.sleep(2000);
        // 退出Executor
        service.shutdown();


    }
}
